<?php

/**
 * myopposites actions.
 *
 * @package    LYPO
 * @subpackage myopposites
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 9301 2008-05-27 01:08:46Z dwhittle $
 */
class myoppositesActions extends sfActions
{
	public function preExecute(){
		if(sfConfig::get('sf_web_debug')==true){
			$this->setLayout('layout');
		}else{
			$this->setLayout(false);
		}
		$this->setTemplate('default');
	}

	/**
	 * Executes Perfectopposites action
	 *
	 * @param sfRequest $request A request object
	 */
	public function executePerfectopposites(sfWebRequest $request)
	{
		$result = new LypoResult();
		$userid = $request->getParameter('userid');
		if(!$userid){
			$result->setErrorMessage("No userid given!");
			return $this->renderText(print_r($result->toArray(),1));
		}
		try {
			/* @var $user User */
			$user = UserPeer::getByContainerString($userid);
			if($user){
				$perfect_opposites = $user->getPerfectOpposites(5);
				$this->args = array('user' => $user, 'userid' => $userid, 'perfect_opposites' => $perfect_opposites, 'basehost' => $request->getHost());
				
				$this->partial = 'perfect_opposites';
			}else{
				$result->setErrorMessage("User not found");
				return $this->renderText(print_r($result->toArray(),1));
			}
		}catch( Exception $e){
			$result->setErrorMessage($e->getMessage());
			return $this->renderText(print_r($result->toArray(),1));
		}
	}
	
/**
	 * Executes Opposites action
	 *
	 * @param sfRequest $request A request object
	 */
	public function executeOpposites(sfWebRequest $request)
	{
		$result = new LypoResult();
		$userid = $request->getParameter('userid');
		$page = $request->getParameter('page',1);
		$active_tab = $request->getParameter('active');
		$active_tab = $active_tab != '' ? $active_tab : 'opposites';

		if(!$userid){
			$result->setErrorMessage("No userid given!");
			return $this->renderText(print_r($result->toArray(),1));
		}
		try {
			/* @var $user User */
			$user = UserPeer::getByContainerString($userid);
			if($user){
				$opposites_pager = $user->getOppositesPager($page,5);
				$this->args = array('user' => $user, 'active_tab' => $active_tab, 'userid' => $userid, 'opposites_pager' => $opposites_pager, 'basehost' => $request->getHost());
				
				//Respected by
				$data = $user->getMyRespectedUsers();
				$this->args['respected_users'] = $data['users'];
				$this->args['respected_respects'] = $data['respects'];
				
				
				$this->partial = 'opposites';
			}else{
				$result->setErrorMessage("User not found");
				return $this->renderText(print_r($result->toArray(),1));
			}
		}catch( Exception $e){
			$result->setErrorMessage($e->getMessage());
			return $this->renderText(print_r($result->toArray(),1));
		}
	}	
	
	
}
